/*
 * Copyright (C) 2013 AdminsProL2
 * 
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */

package com.l2jserver.gameserver.model.actor.instance;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Logger;

import com.l2jserver.Config;
import com.l2jserver.L2DatabaseFactory;
import com.l2jserver.gameserver.datatables.ExperienceTable;
import com.l2jserver.gameserver.datatables.SkillTable;
import com.l2jserver.gameserver.model.skills.L2Skill;

/**
 * @autor Fissban
 * @Fuente: http://www.adminsprol2.com/t38190-sistema-vip-by-me
 */

public class L2AioInstance
{
	private static final Logger _log = Logger.getLogger(L2AioInstance.class.getName());
	
	private L2AioInstance()
	{
	}
	
	public boolean leerAio(L2PcInstance player)
	{
		boolean isAio = false;
		
		try (Connection con = L2DatabaseFactory.getInstance().getConnection();
			PreparedStatement statement = con.prepareStatement("SELECT charId FROM aios WHERE charId=?"))
		{
			statement.setInt(1, player.getObjectId());
			ResultSet rset = statement.executeQuery();
			
			isAio = rset.next();
		}
		catch (SQLException sqle)
		{
			_log.severe("Couldnt get aio data from " + player.getName());
			_log.severe(sqle.getMessage());
			return false;
			
		}
		if (isAio)
		{
			return true;
		}
		return false;
	}
	
	public void addAio(L2PcInstance player)
	{
		// insertar datos en la DB
		updatedb(player, true);
		
		// set lvl
		long pXp = player.getExp();
		long tXp = ExperienceTable.getInstance().getExpForLevel(85);
		player.addExpAndSp(tXp - pXp, 0);
		
	}
	
	public void removeAio(L2PcInstance player)
	{
		updatedb(player, false);
	}
	
	private void updatedb(L2PcInstance player, boolean add)
	{
		String sql = "";
		if (add)
		{
			sql = "INSERT INTO aios (charId) VALUES (?)";
		}
		else
		{
			sql = "DELETE FROM aios WHERE charId = ?";
		}
		
		try (Connection con = L2DatabaseFactory.getInstance().getConnection();
			PreparedStatement statement = con.prepareStatement(sql);)
		{
			statement.setInt(1, player.getObjectId());
			statement.execute();
		}
		catch (SQLException sqle)
		{
			_log.severe("Couldnt get aio data from " + player.getName());
			_log.severe(sqle.getMessage());
		}
	}
	
	public void onEnter(L2PcInstance player)
	{
		if (player.isAio())
		{
			if (Config.AIO_CUSTOM_TITLE_AD)
			{
				player.setTitle(Config.AIO_CUSTOM_TITLE);
			}
			if (Config.AIO_COLOR_NAME_TITLE)
			{
				Integer colorName = Integer.decode("0x" + Config.AIOS_NAMECOLOR);
				Integer colorTitle = Integer.decode("0x" + Config.AIOS_TITLECOLOR);
				player.getAppearance().setNameColor(colorName);
				player.getAppearance().setTitleColor(colorTitle);
			}
			for (int skill : Config.AIO_SKILLS.keySet())
			{
				L2Skill sk = SkillTable.getInstance().getInfo(skill, Config.AIO_SKILLS.get(skill));
				player.addSkill(sk, false);
			}
		}
	}
	
	public static L2AioInstance getInstance()
	{
		return SingleTonHolder._instance;
	}
	
	static class SingleTonHolder
	{
		@SuppressWarnings("synthetic-access")
		static L2AioInstance _instance = new L2AioInstance();
	}
}